package com.ohuang.test_compose.util

import android.content.Context
import android.os.SystemClock
import androidx.annotation.StringRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp



@Composable
fun Number.dp2sp(): TextUnit {
    val current = LocalContext.current
    return dp2sp(current).sp
}

@Composable
fun Modifier.onClick(enabled: Boolean = true, onClick: () -> Unit) = this.clickable(
    enabled = enabled,
    onClick = onClick,
    indication = null,
    interactionSource = remember { MutableInteractionSource() })

@Composable
fun Modifier.onSafeClick(enabled: Boolean = true,time:Long=500, onClick: () -> Unit): Modifier {
    val lastTime = remember {
        mutableStateOf(0L)
    }
    return this.onClick(enabled = enabled, onClick = {
          if (SystemClock.uptimeMillis()-lastTime.value>time){
              onClick()
              lastTime.value= SystemClock.uptimeMillis()
          }
    })
}

@Composable
fun Modifier.paddingWithStatusBar() =
    this.then(
        Modifier.padding(
            top = LocalContext.current.getStatusBarHeight().px2dp(LocalContext.current).dp
        )
    )

@Composable
fun getString(@StringRes id: Int): String {
    return LocalContext.current.getString(id)
}

fun Context.getStatusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (resourceId > 0) {
        resources.getDimensionPixelSize(resourceId)
    } else {
        0
    }
}